<template>
  <div class="sub-category">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter @filter-change="changeFilter" />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort @sort-change="changeSort" />
        <!-- 列表 -->
        <ul>
          <li v-for="item in goodsList" :key="item.id">
            <GoodsItem :goods="item" />
          </li>
        </ul>
        <!-- 加载 -->
        <XtxInfiniteLoading
          :loading="loading"
          :finished="finished"
          @load="onLoad"
        />
      </div>
    </div>
  </div>
</template>
<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item'
import { ref, watch } from 'vue'
import { findSubCategoryGoods } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    // 1. 基础布局
    // 2. 无限加载组件
    // 3. 动态加载数据且渲染
    // 4. 任何筛选条件变化需要更新列表
    const route = useRoute()

    // 请求参数
    let data = {
      page: 1, // 页码
      pageSize: 20 // 页容量
    }

    // 加载状态
    const loading = ref(false)
    // 结束状态
    const finished = ref(false)

    // 商品列表
    const goodsList = ref([])

    // 加载数据
    const onLoad = async () => {
      // 请求前 - 显示请求动画
      loading.value = true
      // 请求中
      const { result } = await findSubCategoryGoods(data)
      // 请求完成 - 隐藏加载动画
      loading.value = false
      // console.log(result)
      // 🔔注意：分页加载时数组追加(不是直接赋值)
      goodsList.value.push(...result.items)
      // debugger
      // 判断是否没有更多数据了
      if (result.items.length < data.pageSize) {
        // 显示没有更多数据了
        finished.value = true
        return
      }
      // 页码需要加1
      data.page++
    }

    // 切换二级分类重新加载
    watch(
      () => route.params.id,
      (id) => {
        if (route.path === '/category/sub/' + id) {
          goodsList.value = []
          data = {
            categoryId: id,
            page: 1,
            pageSize: 20
          }
          finished.value = false
        }
      }
    )

    // 监听筛选区改变
    const changeFilter = (filterParams) => {
      debugger
      goodsList.value = []
      finished.value = false
      data = { ...data, ...filterParams, page: 1 }
      onLoad()
    }
    // 监听排序改变
    const changeSort = (sortParams) => {
      goodsList.value = []
      finished.value = false
      data = { ...data, ...sortParams, page: 1 }
      onLoad()
    }
    return { loading, finished, goodsList, onLoad, changeFilter, changeSort }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
